﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web.Admin
{
    public partial class Admin_decorated_addup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))
                {
                    if (Request.QueryString["action"] == "del")
                    {
                        delData();
                    }
                }
                bindDDL();
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    this.hidId.Value = Request.QueryString["id"];
                    bindData();
                    
                }
            }
        }

        private void bindDDL()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlComList.DataSource = dal.GetCommunity(int.Parse(Session["CommId"].ToString()), true);
            this.ddlComList.DataBind();
            this.ddlBuildList.DataSource = dal.GetBuild(int.Parse(this.ddlComList.SelectedValue),null, true);
            this.ddlBuildList.DataBind();
            this.ddlSelfUnitList.DataSource = dal.GetUnit(int.Parse(this.ddlComList.SelectedValue), int.Parse(this.ddlBuildList.SelectedValue), null);
            this.ddlSelfUnitList.DataBind();
        }
         

        private void delData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                if (dal.Del_decorated(int.Parse(Request.QueryString["id"])))
                {
                    Response.Write("ok");
                }
                else
                {
                    Response.Write("err");
                }
            }
            Response.End();
        }

        private void bindData()
        {
            if (this.hidId.Value != "0")
            {

                DataAccess.PmData dal = new DataAccess.PmData();
                System.Data.DataTable dt = dal.Get_decorated(int.Parse(this.hidId.Value), null);
                if (dt.Rows.Count > 0)
                {
                    this.ddlComList.Items.FindByValue(dt.Rows[0]["Self_Community_Code"].ToString()).Selected = true;
                    this.ddlBuildList.Items.FindByValue(dt.Rows[0]["Self_Build_Code"].ToString()).Selected = true;
                    this.ddlSelfUnitList.Items.FindByValue(dt.Rows[0]["Self_Unit_Code"].ToString()).Selected = true;
                    this.ddlU_Self_No.Items.FindByValue(dt.Rows[0]["U_Self_No"].ToString()).Selected = true;
                    this.txtSelf_Name.Text = dt.Rows[0]["Self_Name"].ToString();
                    this.txtDecoNo.Text = dt.Rows[0]["DecoNo"].ToString();
                    this.txtDeco_Proj.Text = dt.Rows[0]["Deco_Proj"].ToString();
                    this.txtRequest_B_Date.Text = dt.Rows[0]["Request_B_Date"].ToString();
                    this.txtRequest_E_Date.Text = dt.Rows[0]["Request_E_Date"].ToString();
                    this.txtDeco_Corp.Text = dt.Rows[0]["Deco_Corp"].ToString();
                    this.txtDeco_CorpMaster.Text = dt.Rows[0]["Deco_CorpMaster"].ToString();
                    this.txtTele.Text = dt.Rows[0]["Tele"].ToString();
                    this.txtLicense.Text = dt.Rows[0]["License"].ToString();
                    this.txtSum_Deposit.Text = dt.Rows[0]["Sum_Deposit"].ToString();
                    this.txtSum_Reduction.Text = dt.Rows[0]["Sum_Reduction"].ToString();
                    this.txtSum_Return.Text = dt.Rows[0]["Sum_Return"].ToString();
                    this.CBoxCk_1.Checked = Convert.ToBoolean(dt.Rows[0]["Accep_Checked_1"]);
                    this.txtAccep_Date_1.Text = dt.Rows[0]["Accep_Date_1"].ToString();
                    this.txtAccep_Name_1.Text = dt.Rows[0]["Accep_Name_1"].ToString();
                    this.ddlAccep_Remark_1.Items.FindByValue(dt.Rows[0]["Accep_Remark_1"].ToString()).Selected = true;
                    this.CBoxCk_2.Checked = Convert.ToBoolean(dt.Rows[0]["Accep_Checked_2"]);
                    this.txtAccep_Date_2.Text = dt.Rows[0]["Accep_Date_2"].ToString();
                    this.txtAccep_Name_2.Text = dt.Rows[0]["Accep_Name_1"].ToString();
                    this.ddlAccep_Remark_2.Items.FindByValue(dt.Rows[0]["Accep_Remark_2"].ToString()).Selected = true;
                }
            }
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string Self_Community = this.ddlComList.SelectedValue;
            string BuildList = this.ddlBuildList.SelectedValue;
            string SelfUnitList = this.ddlSelfUnitList.SelectedValue;
            string U_Self_No = this.ddlU_Self_No.SelectedValue;
            string Self_Name = this.txtSelf_Name.Text.Trim();
            string DecoNo = this.txtDecoNo.Text.Trim();
            string Deco_Proj = this.txtDeco_Proj.Text.Trim();
            string Request_B_Date = this.txtRequest_B_Date.Text.Trim();
            string Request_E_Date = this.txtRequest_E_Date.Text.Trim();
            string Deco_Corp = this.txtDeco_Corp.Text.Trim();
            string Deco_CorpMaster = this.txtDeco_CorpMaster.Text.Trim();
            string Tele = this.txtTele.Text.Trim();
            string License = this.txtLicense.Text.Trim();
            decimal Sum_Deposit = decimal.Parse(this.txtSum_Deposit.Text.Trim());
            decimal Sum_Reduction = decimal.Parse(this.txtSum_Reduction.Text.Trim());  
            decimal Sum_Return = decimal.Parse(this.txtSum_Return.Text.Trim());
            bool Ck_1 = this.CBoxCk_1.Checked;
            bool Ck_2 = this.CBoxCk_2.Checked;

            string Accep_Date_1 = this.txtAccep_Date_1.Text.Trim();
            string Accep_Name_1 = this.txtAccep_Name_1.Text.Trim();
            string Accep_Remark_1 = this.ddlAccep_Remark_1.SelectedValue;
            string Accep_Date_2 = this.txtAccep_Date_1.Text.Trim();
            string Accep_Name_2 = this.txtAccep_Name_2.Text.Trim();
            string Accep_Remark_2 = this.ddlAccep_Remark_2.SelectedValue;
             

            DateTime Checkdate;
            if (!DateTime.TryParse(Request_B_Date, out Checkdate)) { PM.Web.Common.JsAlert(this, "请输入正确的申请装修时间!"); return; }
            if (!DateTime.TryParse(Request_E_Date, out Checkdate)) { PM.Web.Common.JsAlert(this, "请输入正确的申请截止时间!"); return; }
            if (!DateTime.TryParse(Accep_Date_1, out Checkdate)) { PM.Web.Common.JsAlert(this, "请输入正确的初验日期!"); return; }
            if (!DateTime.TryParse(Accep_Date_2, out Checkdate)) { PM.Web.Common.JsAlert(this, "请输入正确的复验日期!"); return; }
            


            DataAccess.PmData dal = new DataAccess.PmData();
            if (hidId.Value == "0")
            {
                if (dal.Add_face_m_unit_decorated(int.Parse(Self_Community), int.Parse(BuildList), int.Parse(BuildList), int.Parse(U_Self_No), Self_Name, Deco_Proj, DateTime.Parse(Request_B_Date), DateTime.Parse(Request_E_Date), Deco_Corp,
                                            Deco_CorpMaster, License, Tele, Sum_Deposit, Sum_Reduction, Sum_Return, Ck_1, DateTime.Parse(Accep_Date_1), Accep_Name_1, Accep_Remark_1, Ck_2, DateTime.Parse(Accep_Date_2), Accep_Name_2, Accep_Remark_2, Session["Name"].ToString(), DateTime.Now)) ;
                {
                    PM.Web.Common.JsAlert(this, "保存成功！", true);
                }
            }
            else
            {
                if (dal.Modify_face_m_unit_decorated(int.Parse(hidId.Value), int.Parse(Self_Community), int.Parse(BuildList), int.Parse(BuildList), int.Parse(U_Self_No), Self_Name, Deco_Proj, DateTime.Parse(Request_B_Date), DateTime.Parse(Request_E_Date), Deco_Corp,
                                            Deco_CorpMaster, Tele, License, Sum_Deposit, Sum_Reduction, Sum_Return, Ck_1, DateTime.Parse(Accep_Date_1), Accep_Name_1, Accep_Remark_1, Ck_2, DateTime.Parse(Accep_Date_2), Accep_Name_2, Accep_Remark_2, Session["Name"].ToString(), DateTime.Now)) ;
                {
                    PM.Web.Common.JsAlert(this, "修改成功！", true);
                }
            }
        }


    }
}